﻿using System;
using System.Runtime.InteropServices;

namespace DeviceManagement
{
	internal static class DeviceManagement
	{
		const string StorpropLibrary = "Storprop.dll";

		delegate long CdromEnableDigitalPlaybackProc(IntPtr devInfo, ref SP_DEVINFO_DATA deviceInfoData, bool forceUnknown);

		/// <summary>
		/// Enables digital playback for the specified CD-ROM or DVD drive.
		/// </summary>
		public static long CdromEnableDigitalPlayback(IntPtr devInfo, ref SP_DEVINFO_DATA deviceInfoData, bool forceUnknown)
		{
			const string functionName = "CdromEnableDigitalPlayback";

			IntPtr hModule = Kernel32.LoadLibrary(StorpropLibrary);
			IntPtr functionPointer = Kernel32.GetProcAddress(hModule, functionName);
			var proc = (CdromEnableDigitalPlaybackProc) Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(CdromEnableDigitalPlaybackProc));
			return proc(devInfo, ref deviceInfoData, forceUnknown);
		}


		delegate long CdromDisableDigitalPlaybackProc(IntPtr devInfo, ref SP_DEVINFO_DATA deviceInfoData);

		/// <summary>
		/// Disables digital playback for the specified CD-ROM or DVD drive.
		/// </summary>
		public static long CdromDisableDigitalPlayback(IntPtr devInfo, ref SP_DEVINFO_DATA deviceInfoData)
		{
			const string functionName = "CdromDisableDigitalPlayback";

			IntPtr hModule = Kernel32.LoadLibrary(StorpropLibrary);
			IntPtr functionPointer = Kernel32.GetProcAddress(hModule, functionName);
			var proc = (CdromDisableDigitalPlaybackProc) Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(CdromDisableDigitalPlaybackProc));
			return proc(devInfo, ref deviceInfoData);
		}

		delegate long CdromIsDigitalPlaybackEnabledProc(IntPtr devInfo, ref SP_DEVINFO_DATA devInfoData, ref bool enabled);

		/// <summary>
		/// Determines whether digital playback is enabled for the specified CD-ROM or DVD drive.
		/// </summary>
		public static long CdromIsDigitalPlaybackEnabled(IntPtr devInfo, ref SP_DEVINFO_DATA devInfoData, ref bool enabled)
		{
			const string functionName = "CdromIsDigitalPlaybackEnabled";

			IntPtr hModule = Kernel32.LoadLibrary(StorpropLibrary);
			IntPtr functionPointer = Kernel32.GetProcAddress(hModule, functionName);
			var proc = (CdromIsDigitalPlaybackEnabledProc) Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(CdromIsDigitalPlaybackEnabledProc));
			return proc(devInfo, ref devInfoData, ref enabled);
		}

	}
}
